package com.ycuwq.datepicker.date;

import com.ycuwq.datepicker.ResourceTable;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

/**
 * 时间选择器，弹出框
 * Created by ycuwq on 2018/1/6.
 *
 * @deprecated If you need to use it, please use support to achieve.
 */
public class DatePickerDialogFragment extends CommonDialog {
    /**
     * The constant M date picker
     */
    protected DatePicker mDatePicker;
    /**
     * The constant M cancel button
     */
    protected Button mCancelButton;
    /**
     * M decide button
     */
    protected Button mDecideButton;
    /**
     * The constant M selected year
     */
    private int mSelectedYear = -1;
    /**
     * M selected month
     */
    private int mSelectedMonth = -1;
    /**
     * M selected day
     */
    private int mSelectedDay = -1;
    /**
     * The constant M on date choose listener
     */
    private OnDateChooseListener mOnDateChooseListener;
    /**
     * The constant M is show animation
     */
    private boolean mIsShowAnimation = true;

    /**
     * Date picker dialog fragment
     *
     * @param context context
     */
    public DatePickerDialogFragment(Context context) {
        super(context);
        init(context);
    }

    /**
     * Init *
     *
     * @param context context
     */
    private void init(Context context) {
        Component view = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dialog_date, null, false);
        mDatePicker = (DatePicker) view.findComponentById(ResourceTable.Id_dayPicker_dialog);
        mCancelButton = (Button) view.findComponentById(ResourceTable.Id_btn_dialog_date_cancel);
        mDecideButton = (Button) view.findComponentById(ResourceTable.Id_btn_dialog_date_decide);
        mCancelButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                hide();
            }
        });
        mDecideButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (mOnDateChooseListener != null) {
                    mOnDateChooseListener.onDateChoose(mDatePicker.getYear(),
                            mDatePicker.getMonth(), mDatePicker.getDay());
                }
                hide();
            }
        });

        if (mSelectedYear > 0) {
            setSelectedDate();
        }

        setContentCustomComponent(view);
        setAlignment(LayoutAlignment.BOTTOM);
    }

    /**
     * Set on date choose listener *
     *
     * @param onDateChooseListener on date choose listener
     */
    public void setOnDateChooseListener(OnDateChooseListener onDateChooseListener) {
        mOnDateChooseListener = onDateChooseListener;
    }

    /**
     * Show animation *
     *
     * @param show show
     */
    public void showAnimation(boolean show) {
        mIsShowAnimation = show;
    }

    /**
     * Set content custom component common dialog
     *
     * @param component component
     * @return the common dialog
     */
    @Override
    public CommonDialog setContentCustomComponent(Component component) {
        return super.setContentCustomComponent(component);
    }

    /**
     * Set selected date *
     *
     * @param year  year
     * @param month month
     * @param day   day
     */
    public void setSelectedDate(int year, int month, int day) {
        mSelectedYear = year;
        mSelectedMonth = month;
        mSelectedDay = day;
        setSelectedDate();
    }

    /**
     * Set selected date
     */
    private void setSelectedDate() {
        if (mDatePicker != null) {
            mDatePicker.setDate(mSelectedYear, mSelectedMonth, mSelectedDay, false);
        }
    }

    /**
     * On date choose listener
     */
    public interface OnDateChooseListener {
        /**
         * On date choose *
         *
         * @param year  year
         * @param month month
         * @param day   day
         */
        void onDateChoose(int year, int month, int day);
    }
}
